package P1_2;

import java.util.ArrayList;
import java.util.List;

public class CowInFarm {
    public static void main(String[] args) {
        Farm farm = new Farm();
        farm.add(new Cow());
        for (int i = 0; i < 20; i++) farm.newYear();
        System.out.println("20年后，农场有" + farm.getCowCount() + "头牛");
    }
}
class Farm{
    private List<Cow> cows=new ArrayList<>();//牛欄
    private List<Cow> littleCows=new ArrayList<>();
    public void add(Cow cow) {
        cow.setFarm(this);
        cows.add(cow);
    }
    public void addNew(Cow c){
        littleCows.add(c);
    }
    public void newYear() {
        for(Cow c:cows)c.newYear();
        for(Cow c:littleCows)cows.add(c);
        littleCows.clear();
    }

    public int getCowCount() {
        return cows.size();
    }
}
class Cow{
    private int age;
    private Farm farm;
    public Cow(){
        age=0;
    }
    public void newYear() {
        age++;
        if(age>=5) {
            Cow cow = new Cow();
            cow.setFarm(farm);
            farm.addNew(cow);
        }
    }

    public void setFarm(Farm farm) {
        this.farm=farm;
    }
}
